package fishcms

import fishcms.utils.DateUtils

class Post {

    /**
     * 原文
     */
    Post original

    Language language

    Date dateCreated

    Date lastUpdated

    Category category

    Set tags

    /**
     * 置顶设置
     */
    PostTopSet topSet = new PostTopSet()

    String title

    String author

    String content

    static hasMany = [tags: Tag]

    static embedded = ['topSet', 'language']

    static mapping = {
        table "posts"
        tags joinTable: [name: 'post_tag', column: 'tag']
    }

    static constraints = {
        original nullable: true
        content nullable: false, blank: false
        title nullable: false, blank: false
        category nullable: false
    }

    def beforeInsert() {
        if (topSet.top && topSet.abolishedDate == null) {
            topSet.abolishedDate = DateUtils.MAX
        }
    }

    /**
     * 是否置顶
     * @return
     */
    public boolean isTop() {
        topSet.top && topSet.abolishedDate.before(DateUtils.NOW)
    }

    public Date getTopAbolishedDate() {
        topSet?.abolishedDate
    }

}
